package com.xzs.data.controller;


import com.alibaba.fastjson.JSONObject;
import com.xzs.data.dao.ReturnData;
import com.xzs.data.dao.ReturnDataDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class WebDemoDateContrlller {

    @Autowired
    private ReturnDataDao returnDataDao;

    @RequestMapping(value = "setUriAndValue", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    public String setUriAndValue(String uri, String dataValue) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        ReturnData returnData = new ReturnData();
        returnData.setId(uuid);
        returnData.setUri(uri);
        returnData.setReturnData(dataValue);
        returnDataDao.add(returnData);
        return "插入成功！";
    }

    @RequestMapping(value = "selectAll", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    public String selectAll() {
        return JSONObject.toJSONString(returnDataDao.selectAll());
    }

    @RequestMapping(value = "update", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    public String update(String id, String returnData, String uri) {
        ReturnData returnDataObject = new ReturnData();
        returnDataObject.setId(id);
        returnDataObject.setUri(uri);
        returnDataObject.setReturnData(returnData);
        return String.valueOf(returnDataDao.update(returnDataObject));
    }

    @RequestMapping(value = "delete", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    public String delete(String id) {
        return String.valueOf(returnDataDao.delete(id));
    }

}
